// pages/studentRegistPage/studentRegistPage.js
var Mcaptcha = require('../../utils/mcaptcha.js');
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    studentId:undefined,
    studentEmail:undefined,
    password:undefined,
    ensurepassword:undefined,
    passwordFlag:undefined,
    ensureFlag:undefined,
    idFlag:undefined,
    emailFlag:undefined,
    bindCode: undefined,
    name:undefined,
    nameFlag:undefined,
    codeFlag: -2
  },
  getBindCode: function (e) {
    this.setData({
      bindCode: e.detail.value
    })
    this.checkBindCode();
  },
  checkBindCode: function () {
    var that = this
    var iscode = this.mcaptcha.validate(this.data.bindCode);
    if (this.data.bindCode == undefined) {
      that.setData({
        codeFlag: 0
      })
    } else if (iscode) {
      that.setData({
        codeFlag: 1
      })
    } else {
      that.setData({
        codeFlag: -1
      })
    }
    console.log(this.data.codeFlag);
  },
  changeBind() {
    this.mcaptcha.refresh();
  },

  goStudentAcPage: function () {
    wx.redirectTo({
      url: '/pages/studentAcPage/studentAcpage',
    })
  },
  goLogin: function () {
    wx.redirectTo({
      url: '/pages/studentLogPage/studentLogPage',
    })
  },
 
  getStudentEmail: function (e) {
    if (e.detail.value) {
      this.setData({
        studentEmail: e.detail.value
      })
    }
    var that =this;
    var currentFlag=1;
    var isValid=this.isValidEmail(this.data.studentEmail);
    if (isValid){
      that.setData({
        emailFlag:currentFlag
      })
    }
    else{
      wx.showToast({
        title: '请输入正确邮箱',
        icon: "none",
      })
    }
  },

  getName: function (e) {
    if (e.detail.value) {
      this.setData({
        name: e.detail.value
      })
    }
    var that =this;
    var currentFlag = 1;
    var isValid = this.isValidName(this.data.name);
    if (isValid) {
      that.setData({
        nameFlag: currentFlag
      })
    }
    else {
      wx.showToast({
        title: '请输入正确姓名',
        icon: "none",
      })
    }
},
 
  isValidEmail: function (str) {
    var reg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },

  isValidName: function (str) {
    var reg = /^[\u4e00-\u9fa5 ]{2,4}$/;
    if (reg.test(str)) {
      return true;
    }
    return false;
  },

  isValidPassword: function (str) {
    if ((str.length < 6) || (str.length > 20)) {
      return false;
    }

    if (/[^a-zA-Z0-9_]/.test(str)) {

      return false;

    }

    if (/(^[a-z]+$|^[A-Z]+$|^\d+$|^_+$)/.test(str)) {

      return false;

    }

    return true;

  },

  getPassword: function (e) {
    if (e.detail.value) {
      this.setData({
        password: e.detail.value
      })
    }
    var isLegal=this.isValidPassword(this.data.password);
    var that = this;
    if(isLegal){
      that.setData({
        passwordFlag:1
      })
    }
    else{
      wx.showToast({
        title: '密码应在6-20位间，包含下划线、字母、数字中的至少两种',
        icon: "none",
      })
    }
  },
  getStudentId: function (e) {
    if (e.detail.value) {
      this.setData({
        studentId: e.detail.value
      })
      var currentFlag = 0;
      var isLegal=0;
      var that = this;
      var reg = /^[0-9]{13}$/g;
      if(!reg.test(this.data.studentId)){
        wx.showToast({
          title: '请输入合法学号，13位数字',
          icon: "none",
        })
      }
      else{
        isLegal=1;
      }
      
      if(isLegal)
      { wx.request({//检测学号是否已存在 
        url: 'https://weixin0304.cn/isStudentIdExist.php',
        header: {
          'content-type':'application/x-www-form-urlencoded'
        },
        method:'POST',
        data: {
          studentId: this.data.studentId,
        },

        success: function (res) {
          console.log(res);
          if (res.data== 0) {//这里待改
            //学号合法
            currentFlag = 1;
            that.setData({
              idFlag: currentFlag
            })
          } else {
            //用户名非法（学号在数据库已存在）
            currentFlag = 0;
            wx.showToast({
              title: '该学号已被注册',
              icon: "none",
            })
            that.setData({
              idFlag: currentFlag
            })
          }
        }
      })
    } 
  }
    
},
  
  getEnsurePassword: function (e) {
    if (e.detail.value) {
      this.setData({
        ensurepassword: e.detail.value
      })
      var currentFlag = 1
      var that = this;
      if (this.data.ensurepassword!=this.data.password) {
        wx.showToast({
          title: '两次密码不一致',
          icon: "none"
        })
        currentFlag = 0
        that.setData({
          ensureFlag: currentFlag
        })
      } else {
        if(this.data.password){
        that.setData({
          ensureFlag: currentFlag
        })
        }
      }
    }
  },

  regist: function () {
    if ((undefined != this.data.password) && (undefined != this.data.studentEmail) && (undefined != this.data.studentId) && (undefined != this.data.ensurepassword)&&this.data.bindCode&&this.data.name){
  
      if (this.data.codeFlag == -1) {
        wx.showToast({
          title: '请输入正确验证码',
          icon: "none"
        })
        this.changeBind();
      }
      else if (!this.data.idFlag) {
        wx.showToast({
          title: '请输入合法学号',
          icon: "none"
        })
      }
      else if(!this.data.nameFlag){
        wx.showToast({
          title: '请输入正确姓名',
          icon: "none"
        })
      }
       else if (!this.data.passwordFlag) {
        wx.showToast({/////eeeeeeee
          title: '两次密码不一致',
          icon: "none"
        })
      }
      else if(!this.data.emailFlag){
        wx.showToast({
          title: '请输入正确邮箱',
          icon: "none"
        })
      }
      
      else {
        var that=this
        wx.request({
          url: 'https://weixin0304.cn/studentRegist.php',
          data: {
            studentId: this.data.studentId,
            password: this.data.password,
            studentEmail: this.data.studentEmail,
            name:this.data.name
          },
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          method: 'POST',
          success: function (res) {
            console.log(res);
            if(res.data==1){
            
              wx.showToast({
                title: '注册成功',
                icon: "none"
              })

              //佛祖保佑 不再Bug



              wx.setStorageSync('id', that.data.studentId);
              wx.setStorageSync('name', that.data.name);
              wx.setStorageSync('role',"学生" );
              wx.redirectTo({
                url: "/pages/studentAcPage/studentAcPage?id=" + that.data.studentId + "&name=" + that.data.name,
              })
              wx.set
            }
            else{
              wx.showToast({
                title: '注册失败？？？',
                icon: "none",
              })
            }
  
          }
        
        })
      }
    } else {
      wx.showModal({
        title: '提示',
        content: '请将信息填写完整后提交',
        showCancel: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.mcaptcha = new Mcaptcha({
      el: 'canvas',
      width: 80,
      height: 35,
      createCodeImg: ""
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})